﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Frame.Lib.Web
{
    public delegate void BindViewHandler();
    public class Pager
    {

        private LinkButton btnFirst;
        private LinkButton btnPre;
        private LinkButton btnNext;
        private LinkButton btnLast;
        private DropDownList drpPages;
        private BindViewHandler BindView;
        private PagerSortSetting setting;

        private WebControl _container;

        public PagerSortSetting Setting
        {
            get { return setting; }
        }


        public Pager(WebControl container, BindViewHandler BindView, System.Web.UI.StateBag viewState)
        {
            _container = container;
            this.setting = new PagerSortSetting(viewState);

            this.setting.PageCountChanged += new PageCountChangeHandar(setting_PageCountChanged);


            this.BindView = BindView;
            btnFirst = (LinkButton)container.FindControl("btnFirst");
            btnPre = (LinkButton)container.FindControl("btnPre");
            btnNext = (LinkButton)container.FindControl("btnNext");
            btnLast = (LinkButton)container.FindControl("btnLast");
            drpPages = (DropDownList)container.FindControl("drpPages");

            btnFirst.Click += new EventHandler(btnFirst_Click);
            btnPre.Click += new EventHandler(btnPre_Click);
            btnNext.Click += new EventHandler(btnNext_Click);
            btnLast.Click += new EventHandler(btnLast_Click);
            drpPages.AutoPostBack = true;
            drpPages.SelectedIndexChanged += new EventHandler(drpPages_SelectedIndexChanged);
        }

        void setting_PageCountChanged(object sender)
        {
            SetPager();
        }


        void drpPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            setting.PageIndex = int.Parse(drpPages.SelectedValue);
            BindView();
        }
        void btnNext_Click(object sender, EventArgs e)
        {
            setting.PageIndex++;
            BindView();
        }

        void btnPre_Click(object sender, EventArgs e)
        {
            setting.PageIndex--;
            BindView();
        }

        void btnFirst_Click(object sender, EventArgs e)
        {
            setting.PageIndex = 1;
            BindView();
        }
        void btnLast_Click(object sender, EventArgs e)
        {
            setting.PageIndex = setting.PageCount;
            BindView();
        }

        private void SetPager()
        {

            int pageCount = setting.PageCount;
            int pageIndex = setting.PageIndex;
            drpPages.Items.Clear();
            for (int i = 1; i <= pageCount; i++)
            {
                ListItem item = new ListItem(i + " / " + pageCount, i.ToString());
                drpPages.Items.Add(item);
            }
            drpPages.SelectedValue = pageIndex.ToString();

            if (setting.PageIndex <= 1)
            {
                //btnPre.Enabled = false;
                //btnFirst.Enabled = false;

                btnPre.Attributes.Add("onclick", "return false");
                btnPre.Style.Add("color", "#dddddd");

                btnFirst.Attributes.Add("onclick", "return false");
                btnFirst.Style.Add("color", "#dddddd");
            }
            if (setting.PageIndex >= setting.PageCount)
            {
                //btnNext.Enabled = false;
                //btnLast.Enabled = false;

                btnNext.Attributes.Add("onclick", "return false");
                btnNext.Style.Add("color", "#dddddd");

                btnLast.Attributes.Add("onclick", "return false");
                btnLast.Style.Add("color", "#dddddd");
            }
            if (setting.PageIndex > 1)
            {
                //btnPre.Enabled = true;
                //btnFirst.Enabled = true;

                btnPre.Attributes.Remove("onclick");
                btnPre.Style.Remove("color");

                btnFirst.Attributes.Remove("onclick");
                btnFirst.Style.Remove("color");
            }
            if (setting.PageIndex < setting.PageCount)
            {
                //btnNext.Enabled = true;
                //btnLast.Enabled = true;

                btnNext.Attributes.Remove("onclick");
                btnNext.Style.Remove("color");

                btnLast.Attributes.Remove("onclick");
                btnLast.Style.Remove("color");
            }

            if (pageCount <= 1)
            {
                _container.Visible = false;
            }

        }


        public static void BindDrp(DropDownList drpPager)
        {
            drpPager.AutoPostBack = true;
            ListItem item = new ListItem("每页10行", "10");
            drpPager.Items.Add(item);
            item = new ListItem("每页20行", "20");
            drpPager.Items.Add(item);
            item = new ListItem("每页50行", "50");
            drpPager.Items.Add(item);
            //item = new ListItem("全部显示", "all");
            //drpPager.Items.Add(item);

            drpPager.SelectedValue = "10";
        }
        public static void SetPageSize(PagerSortSetting setting, DropDownList drpPager)
        {
            string strPageSize = drpPager.SelectedValue;
            if (strPageSize == "all")
            {
                setting.PageSize = int.MaxValue;
                setting.PageIndex = 1;
            }
            else
            {
                setting.PageSize = int.Parse(strPageSize);
            }
        }
    }
}
